package control;

import javafx.application.Application;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.ScrollBar;
import javafx.geometry.Orientation;
import javafx.scene.text.Text;

// 滚动条(可用鼠标滑轮滚动)
public class ScrollBarDemo extends Application {
	private BorderPane root = new BorderPane();
	private AnchorPane ap = new AnchorPane();		// 锚点面板
	private Text text;
	private ScrollBar sb1;
	private ScrollBar sb2;
	
	public static void main(String[] args) {
		launch(args);
	}
	
	@Override
    public void start(Stage stage) {
	   
	   // 文本放在Pane和AnchorPane面板中才能移动
	   text = new Text(30, 100, "JavaFX is cool");
	   ap.getChildren().add(text);
	   root.setCenter(ap);
	   
	   // 水平滚动条(默认水平)
	   sb1 = new ScrollBar();
	   root.setBottom(sb1);
		
	   // 竖直滚动条
	   sb2 = new ScrollBar();
	   sb2.setOrientation(Orientation.VERTICAL);   // 竖直移动
	   sb2.setVisibleAmount(50);     // 设置滚动条的宽度(框里的条形的宽度，默认为15)
	   sb2.setUnitIncrement(20);	 // 设置每按一次移动按钮和滚动一次滑轮移动的距离
	   root.setRight(sb2);
	   
	   Scene scene = new Scene(root, 300, 300);
	   stage.setScene(scene);
	   stage.setTitle("ScrollBar");
	   stage.show();
	   
	   // 监听器
	   sb1.valueProperty().addListener(e -> {
		   double scale = root.getWidth()/sb1.getMax();
		   text.setX(sb1.getValue() * scale);        // 获取滚动条的当前位置(默认为0)
	   });  
	   
	   sb2.valueProperty().addListener(e -> {
		   double scale = root.getWidth()/sb2.getMax();
		   text.setY(sb2.getValue() * scale);
	   });
    }
}


